package com.shenyong.ladmin.result;

import java.util.HashMap;
import java.util.Map;

/**
 * Create By:qiang
 * Date: 2019/11/29 11:33
 * 文件说明: TinyURL
 */
public class TinyURL {
    public static final char[] array = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'q', 'w', 'e', 'r', 't', 'y',
            'u', 'i', 'o', 'p', 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'z', 'x', 'c', 'v', 'b', 'n', 'm', 'Q',
            'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'Z', 'X', 'C',
            'V', 'B', 'N', 'M' };

    public static Map<Character, Integer> charValueMap = new HashMap<Character, Integer>();

    //初始化map
    static {
        for (int i = 0; i < array.length; i++)
            charValueMap.put(array[i], i);
    }

    public static void main(String[] args) {

    }
    /**
     * 得到URL返回
     * @return URL
     */
    public String getLongStrUrl() {
        String str = Math.random() + "";
        String str2 = Math.random() + "";
        str2 = str2.substring(2);
        str = str.substring(2);
        String str3 = str + str2;
        str3 = str3.substring(0, 18);
        long number = Long.parseLong(str3);
        String decimalStr = numberConvertToDecimal(number, 62);
        return decimalStr;
    }
    /**
     * 把数字转换成相对应的进制,目前支持(2-62)进制
     *
     * @param number
     * @param decimal
     * @return
     */
    public static String numberConvertToDecimal(long number, int decimal) {
        StringBuilder builder = new StringBuilder();
        while (number != 0) {
            builder.append(array[(int) (number - (number / decimal) * decimal)]);
            number /= decimal;
        }
        return builder.reverse().toString();
    }

    /**
     * 把进制字符串转换成相应的数字
     *
     * @param decimalStr
     * @param decimal
     * @return
     */
    public static long decimalConvertToNumber(String decimalStr, int decimal) {
        long sum = 0;
        long multiple = 1;
        char[] chars = decimalStr.toCharArray();
        for (int i = chars.length - 1; i >= 0; i--) {
            char c = chars[i];
            sum += charValueMap.get(c) * multiple;
            multiple *= decimal;
        }
        return sum;
    }
}
